# sbe.mk - Combining file for inclusion in makefiles.
# Copyright (C) 2007  Mikhail Vorozhtsov
# See the LICENSE section of the README file for details.

# $Id$

include $(SBE_BASE)/mk/functions.mk
include $(SBE_BASE)/mk/rules.mk

SBE_VER_MAJOR = 0
SBE_VER_MINOR = 0

define SBE_CHECK_VER
  ifneq ($(3),$$($(2)_VER_MAJOR))
    $$(error Incompatible $(1) major version)
  endif
  ifneq ($$(shell test $(4) -le $$($(2)_VER_MINOR) && echo yes),yes)
    $$(error Incompatible $(1)-$(3) minor version)
  endif
endef

define SBE_INIT
  $$(eval $$(call SBE_CHECK_VER,SBE,SBE,$(1),$(2)))
  SBE_DEP_LEVEL = SBE_ZERO
  ifneq ($(3),)
    ifndef TARGET
      $$(error TARGET is not defined)
    endif
  endif
  ifeq ($(3),arch)
    ARCH := $$(TARGET)
  endif
  ifeq ($(3),arch+)
    ifeq ($$(findstring -,$$(TARGET)),)
      ARCH := $$(TARGET)
    else
      ARCH := $$(call extract-arch,$$(TARGET))
      PLATFORM := $$(call extract-platform,$$(TARGET))
    endif
  endif
  ifeq ($(3),machine)
    ARCH := $$(call extract-arch,$$(TARGET))
    PLATFORM := $$(call extract-platform,$$(TARGET))
  endif
  ifneq ($(3),)
    ifeq ($$(call if-file-exists,mk/arch/$$(ARCH).mk),)
      $$(error Architecture '$$(ARCH)' is not supported)
    endif
  endif
  SBE_CPPFLAGS := \
    $$(if $$(ARCH),-D__ARCH__=$$(ARCH) \
                   -DARCH_IS_$$(call upper-case,$$(ARCH)),) \
    $$(if $$(PLATFORM),-D__PLATFORM__=$$(PLATFORM) \
                       -DPLATFORM_IS_$$(call upper-case,$$(PLATFORM)),) \
    -include "$$(SBE_BASE)/headers/build/env.hh"
  OBJCOPY ?= objcopy
  SED ?= sed
endef

sbe-init = $(eval $(call SBE_INIT,$(1),$(2),$(3)))

define SBE_REQ
  $$(eval $$(call SBE_CHECK_VER,SBE,SBE,$(1),$(2)))
  ifneq ($(3),)
    ifndef ARCH
      $$(error ARCH is not defined)
    endif
  endif
  ifeq ($(3),machine)
    ifndef PLATFORM
      $$(error PLATFORM is not defined)
    endif
  endif
endef

sbe-req = $(eval $(call SBE_REQ,$(1),$(2),$(3)))

define SBE_MACHINE_INIT
  include mk/arch/$$(ARCH).mk
  ifdef PLATFORM
    ifeq ($$(findstring " $$(PLATFORM) "," $$(PLATFORMS) "),)
      $$(error \
Platform '$$(PLATFORM)' is not supported on architecture '$$(ARCH)')
    endif
    include mk/platform/$$(PLATFORM).mk
    -include mk/machine/$$(ARCH)-$$(PLATFORM).mk
  endif
endef

sbe-machine-init = $(eval $(call SBE_MACHINE_INIT))

define SBE_SELF
  ifdef $$(SBE_DEP_LEVEL)__EXPECT
    ifneq ($(2),$$($$(SBE_DEP_LEVEL)__EXPECT))
      $$(info [$(2)] [$$($$(SBE_DEP_LEVEL)__EXPECT)])
      $$(error \
$(2)_BASE does not point to \
$$($$(SBE_DEP_LEVEL)__EXPECT_NAME)-$$($$(SBE_DEP_LEVEL)__EXPECT_VER_MAJOR) \
source tree)
    endif
    ifneq ($$(shell test $(4) -le $$($$(SBE_DEP_LEVEL)__EXPECT_VER_MINOR) \
                    && echo yes),yes)
      $$(error Incompatible \
$$($$(SBE_DEP_LEVEL)__EXPECT_NAME)-$$($$(SBE_DEP_LEVEL)__EXPECT_VER_MAJOR) \
minor version)
    endif
  endif
  $(2)_NAME = $(1)
  $(2)_NAME_UC = $(call upper-case,$(1))
  $(2)_VER_MAJOR = $(3)
  $(2)_VER_MINOR = $(4)
  $(call upper-case,$(1))_MAJORS += $(3)
  $$(SBE_DEP_LEVEL)__SELF = $(2)
  SELF = $(2)
endef

sbe-self = $(eval $(call SBE_SELF,$(1),$(call upper-case,$(1))_$(2),$(2),$(3)))

define SBE_DEP
  ifndef $(2)_LOADED
    ifndef $(2)_BASE
      $$(error $(2)_BASE is not defined)
    endif
    SBE_DEP_LEVEL := $$(SBE_DEP_LEVEL)_DEP
    $$(SBE_DEP_LEVEL)__EXPECT = $(2)
    $$(SBE_DEP_LEVEL)__EXPECT_NAME = $(1)
    $$(SBE_DEP_LEVEL)__EXPECT_VER_MAJOR = $(3)
    $$(SBE_DEP_LEVEL)__EXPECT_VER_MINOR = $(4)
    include $$($(2)_BASE)/mk/dep.mk
    SBE_DEP_LEVEL := $$(patsubst %_DEP,%,$$(SBE_DEP_LEVEL))
    SELF := $$($$(SBE_DEP_LEVEL)__SELF)
    $(2)_LOADED = yes
  else
    $$(eval $$(call SBE_CHECK_VER,$(1)-$(3),$(2),$(3),$(4)))
  endif
  $$(SBE_DEP_LEVEL)_$(1) = $(2)
endef

sbe-dep = $(eval $(call SBE_DEP,$(1),$(call upper-case,$(1))_$(2),$(2),$(3)))
sbe-dep-var = $($($(SBE_DEP_LEVEL)_$(1))_$(2))

define SBE_ENV_FNS
  $(1)-src = $$(call prefix-src,$(1),$$(1))
  $(1)-arch-src = $$(call prefix-arch-src,$(1),$$(1))
  $(1)-platform-src = $$(call prefix-platform-src,$(1),$$(1))
  $(1)-machine-src = $$(call prefix-machine-src,$(1),$$(1))
endef

sbe-env-fns = $(eval $(call SBE_ENV_FNS,$(1)))

